from receiver.topic_handler import TopicHandler
from px4.drone_controller import DroneController


droneController = DroneController()


class DroneControlTopicHandler(TopicHandler):
    def __init__(self):
        super().__init__()

    def enable_handle(self, topic: str) -> bool:
        return "/drc/down"  in topic

    async def handle(self, message: dict) -> int:
        try:
            self.bid = self.get_bid(message)
            self.method = self.get_method(message)
            if self.method == "drone_control":
                await droneController.handle_control(self.get_data(message))
            elif self.method == "start_offboard_mode":
                await droneController.start_offboard_mode()
            elif self.method == "stop_offboard_mode":
                await droneController.stop_offboard_mode()
            else:
                print(f"Unknown method: {self.method}")
                return 1
        except RuntimeError as e:
            print(f"Error handling message: {str(e)}")
            return 1